1110G - Tree-Tac-Toe - CodeForces Solution


constructive algorithms games trees *3100

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

int dfs(int x, int f, const vector<vector<int>> &G, char* c, vector<bool> &vis, int &ans) {
	if (vis[x]) return -1;
	vis[x] = true;
	if (ans) return -1;

	int ws = -1;

	int cw = (c[x] == 'W' || ws != -1);
	int rt = 0, c0 = 0, c1 = 0;
	for (int y : G[x]) {
		if (y == f || y == ws) continue;
		int ret = dfs(y, x, G, c, vis, ans);
		if (ret == 0) {
			if (cw && (G[x].size() >= 2 && ws == -1 || G[x].size() >= 3)) {
				ans = 1;
			}
			if (c0) ans = 1;
			rt += 1;
			c0 += 1;
		} else if (ret == 1) {
			ans |= cw;
			if (c1) ans = 1;
			c1 += 1;
			rt += 1;
		}
	}

	if (!rt && !cw) return -1;
	return c0 ? 1 : 0;
}

int main() {
	int T;
	scanf("%d", &T);
	for (int _ = 0; _ < T; ++_) {
		int n;
		scanf("%d", &n);
		vector<vector<int>> G(n);
		vector<int> nc(n), ss(n), aw(n);
		vector<bool> vis(n);
		for (int i = 0; i < n - 1; ++i) {
			int x, y;
			scanf("%d %d", &x, &y);
			x -= 1;
			y -= 1;
			G[x].push_back(y);
			G[y].push_back(x);
		}

		int ans = 0;
		auto c = unique_ptr<char[]>(new char[n + 1]);
		scanf("%s", c.get());
		for (int x = 0; x < n; ++x) {
			if (G[x].size() >= 4) goto WW;
		}

		for (int x = 0; x < n; ++x) {
			for (int y : G[x]) {
				ss[x] += (G[y].size() > 1);
				if (c[y] == 'W') continue;
				for (int z : G[y]) {
					if (z == x) continue;
					if (G[z].size() >= 3) goto NL;
				}
				nc[x] += 1;
NL:
				;
			}

			int m = G[x].size(), wc = m - nc[x];
			if (m >= 4 || c[x] == 'W' && m >= 3) goto WW;
			if (wc >= 2 || wc == 1 && c[x] == 'W' && nc[x] >= 1) goto WW;
			if (wc == 1 && nc[x] >= 2) goto WW;
		}

		for (int x = 0; x < n; ++x) {
			int m = G[x].size();
			if (c[x] == 'W') {
				if (ss[x] >= 1 && m >= 2) goto WW;
			} else {
				if (ss[x] >= 2 && m >= 3) goto WW;
			}
		}

		for (int x = 0; x < n; ++x) {
			for (int i = 0; i < (int)G[x].size(); ++i) {
				int y = G[x][i];
				int cn = 0;
				for (int z : G[y]) {
					if (z == x) continue;
					if (G[z].size() > 1) cn = -10;
					cn += 1;
				}
				if (cn == 2) {
					G[x].erase(G[x].begin() + i);
					for (int j = 0; j < (int)G[y].size(); ++j) {
						if (G[y][j] == x) {
							G[y].erase(G[y].begin() + j);
							break;
						}
					}
					c[x] = 'W';
					break;
				}
			}
		}

		for (int i = 0; i < n; ++i) dfs(i, -1, G, c.get(), vis, ans);
		if (ans) WW: puts("White");
		else puts("Draw");
	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST